package com.runchinaup.smartbelt.domian;

/**
 * Created by liber on 2018/2/2.
 */


import android.util.Log;
import android.widget.CheckBox;

/**
 * 设置闹钟的时间
 */
public class AlarmData extends BaseData {
    //闹钟的头
    private byte alarmOne = (byte) 0xa1;
    private byte alarmTwo = (byte) 0x5a;
    private byte alarmThree = (byte) 0x09;
    private byte alarmFour = (byte) 0x04;

    //设置的参数
    private byte alarmNum;                  //闹钟编号
    private byte alarmHour = 0;             //设置时
    private byte alarmMintue = 0;           //设置分
    private byte alarmSecond = 0;           //设置秒
    private byte alarmWeek = 127;                 //设置星期


    public byte getAlarmNum() {
        return alarmNum;
    }

    public void setAlarmNum(byte alarmNum) {
        this.alarmNum = alarmNum;
    }

    public byte getAlarmHour() {
        return alarmHour;
    }

    public void setAlarmHour(byte alarmHour) {
        this.alarmHour = alarmHour;
    }

    public byte getAlarmMintue() {
        return alarmMintue;
    }

    public void setAlarmMintue(byte alarmMintue) {
        this.alarmMintue = alarmMintue;
    }

    public byte getAlarmWeek() {
        return alarmWeek;
    }

    public void setAlarmWeek(byte alarmWeek) {
        this.alarmWeek = alarmWeek;
    }

    public int getWeekResult(CheckBox[] week) {
        int result = 0;
        result += week[0].isChecked() ? 1 : 0;
        for (int i = 1; i < 7; i++) {
            result += Math.pow(week[i].isChecked() ? 2 : 0, i);
        }
        Log.e("getWeekResult: ", result + "");
        return result;
    }

    public byte[] everyDay() {
        byte weekData = alarmWeek;
        byte[] every = new byte[7];
        for (int i = 0; i < 7; i++) {
            every[i] = (byte) (weekData % 2);
            weekData = (byte) (weekData / 2);
        }
        StringBuilder sb = new StringBuilder();
        for (byte b : every) {
            sb.append(String.format("%01d", (b & 0xff)));
        }
        Log.e("everyDay: ", sb.toString());

        return every;
    }

    //设置闹钟
    public byte[] remindByAlarm() {

        //四个报头 + 闹钟编号 + 时 + 分 + 秒 + 星期 + 校验码
        byte[] setValue = new byte[]{
                alarmOne,
                alarmTwo,
                alarmThree,
                alarmFour,
                alarmNum,
                alarmHour,
                alarmMintue,
                alarmSecond,
                alarmWeek,
                0
        };
        setValue[setValue.length - 1] = CountCheckCode(setValue);

        return setValue;
    }

    //读取闹钟时间    i代表第几个闹钟
    public byte[] getAlarmTime(int i) {
        byte[] setValue = new byte[]{
                alarmOne,
                alarmTwo,
                0x05,
                0x40,
                (byte) i,
                0
        };

        switch (i){
            case 0:
                setValue[setValue.length-1] = (byte) 0xc0;
                break;
            case 1:
                setValue[setValue.length-1] = (byte) 0xbf;
                break;
            case 2:
                setValue[setValue.length-1] = (byte) 0xbe;
                break;
        }
        return setValue;
    }
}
